package com.cxb.servicemap.remote;

import com.cxb.internalcommon.constant.AmapConfigConstants;
import com.cxb.internalcommon.responese.DirectionResponse;
import com.cxb.servicemap.controller.DirectionController;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import sun.plugin.com.AmbientProperty;

import java.awt.image.RescaleOp;

@Service
@Slf4j
public class MapDirectionClient {

    /*
    * https://restapi.amap.com/v3/direction/driving?
    * origin=116.481028,39.989643&destination=116.465302,40.004717&extensions=all&output=json&
    * key=7a1788aaf0d35c0d7259e918cddf57f1*/
    @Value("${amap.key}")
    private String amapKey;

    @Autowired
    private RestTemplate restTemplate;
    public DirectionResponse direction(String depLongitude, String depLatitude , String destLongitude, String destLatitude){

        StringBuilder urlBuild = new StringBuilder();
        urlBuild.append(AmapConfigConstants.DIRECTION_URL);
        urlBuild.append("?");
        urlBuild.append("origin="+depLongitude+","+depLatitude);
        urlBuild.append("&");
        urlBuild.append("destination="+destLongitude+","+destLatitude);
        urlBuild.append("&extensions=base&output=json&");
        urlBuild.append("key="+amapKey);
        //log.info(urlBuild.toString());

        //调用高德接口
        ResponseEntity<String> directionEntity = restTemplate.getForEntity(urlBuild.toString(), String.class);
        String body = directionEntity.getBody();
        //log.info("高德"+body);

        //解析接口
        DirectionResponse directionResponse = parseDirectionEntity(body);
        return directionResponse;
    }

    private DirectionResponse parseDirectionEntity(String body){
        DirectionResponse directionResponse = null;

        try {

            //最外层
            JSONObject result = JSONObject.fromObject(body);
            if(result.has(AmapConfigConstants.STATUS)){
                int status = result.getInt(AmapConfigConstants.STATUS);
                if(status==1){
                    if(result.has(AmapConfigConstants.ROUTE)){
                        JSONObject routeObject = result.getJSONObject(AmapConfigConstants.ROUTE);
                        JSONArray pathArray = routeObject.getJSONArray(AmapConfigConstants.PATHS);
                        JSONObject pathObject = pathArray.getJSONObject(0);
                        directionResponse = new DirectionResponse();
                        if(pathObject.has(AmapConfigConstants.DISTANCE)){
                            int distance = pathObject.getInt(AmapConfigConstants.DISTANCE);
                            directionResponse.setDistance(distance);
                        }
                        if(pathObject.has(AmapConfigConstants.DURATION)){
                            int duration = pathObject.getInt(AmapConfigConstants.DURATION);
                            directionResponse.setDuration(duration);
                        }

                    }
                }
            }

        } catch (Exception e) {

        }
        return directionResponse;
    }
}
